﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Windows.Forms;

namespace tracentsure
{
    class AppUitls
    {
        MsSqlHelper mssql = new MsSqlHelper();
        public string XS()
        {
            string sql = "";
            string result = "";
            string url = "/open-api/dhs/org";
            try
            {
                sql = "select top 10 case when a.djbs='XSA' then '2-0' else '1-1' end as bizType,rtrim(c.yingyzz) as directionOrgCreditCode,rtrim(c.dwmch) as directionOrgName,"
                + " '' as handoverOrgCreditCode,''as handoverOrgName,'' as handoverPerson,'' as handoverTime,'' as logisticsMode,cast(a.rq as date) as operateTime,"
                + "a.djbh as orderNo,'91510106768621824L' as orgCreditCode,'四川金仁医药集团有限公司' as orgName,'91510106768621824L' as sourceOrgCreditCode,'四川金仁医药集团有限公司' as sourceOrgName,"
                + "0 as temperatureFlag,'' as traceCodeList,'' as transportationMode,'System' as [user] "
                + " from pf_ckhz a,pf_ckmx b,mchk c,spkfk d "
                + " where a.djbs in('XSA','XSC') and a.djbh=b.djbh and a.dwbh=c.dwbh and b.spid=d.spid and d.shengccj like '%哈药%'";
                DataTable dt = mssql.ExecuteDt(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    var entitydt = DataConvert<ClassModel>.ToEntity(dr);
                    string sqldetail = "select spid from pf_ckmx where djbh='" + dr["orderNo"].ToString() + "'";
                    DataTable dtdetail = mssql.ExecuteDt(sqldetail);
                    List<string> list = new List<string>();
                    foreach (DataRow drdet in dtdetail.Rows)
                    {
                        list.Add(drdet[0].ToString());
                    }
                    entitydt.traceCodeList = list;
                    IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
                    result = JsonConvert.SerializeObject(entitydt, Newtonsoft.Json.Formatting.Indented, timeFormat);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return result;
        }
    }
}